/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.examples.javaeditor.java;


import java.io.*;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.SingleLineRule;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WhitespaceRule;
import org.eclipse.jface.text.rules.WordRule;

import org.eclipse.ui.examples.javaeditor.util.JavaColorProvider;
import org.eclipse.ui.examples.javaeditor.util.JavaWhitespaceDetector;
import org.eclipse.ui.examples.javaeditor.util.JavaWordDetector;

/**
 * A Java code scanner.
 */
public class JavaCodeScanner extends RuleBasedScanner {

	private static String[] fgKeywords= { "abstract", "break", "case", "catch", "class", "continue", "default", "do", "else", "extends", "final", "finally", "for", "if", "implements", "import", "instanceof", "interface", "native", "new", "package", "private", "protected", "public", "return", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "volatile", "while" }; //$NON-NLS-36$ //$NON-NLS-35$ //$NON-NLS-34$ //$NON-NLS-33$ //$NON-NLS-32$ //$NON-NLS-31$ //$NON-NLS-30$ //$NON-NLS-29$ //$NON-NLS-28$ //$NON-NLS-27$ //$NON-NLS-26$ //$NON-NLS-25$ //$NON-NLS-24$ //$NON-NLS-23$ //$NON-NLS-22$ //$NON-NLS-21$ //$NON-NLS-20$ //$NON-NLS-19$ //$NON-NLS-18$ //$NON-NLS-17$ //$NON-NLS-16$ //$NON-NLS-15$ //$NON-NLS-14$ //$NON-NLS-13$ //$NON-NLS-12$ //$NON-NLS-11$ //$NON-NLS-10$ //$NON-NLS-9$ //$NON-NLS-8$ //$NON-NLS-7$ //$NON-NLS-6$ //$NON-NLS-5$ //$NON-NLS-4$ //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$

	private static String[] fgTypes= { "void", "boolean", "char", "byte", "short", "int", "long", "float", "double" }; //$NON-NLS-1$ //$NON-NLS-5$ //$NON-NLS-7$ //$NON-NLS-6$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-2$

	private static String[] fgConstants= { "false", "null", "true" }; //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
	
	
	//user defined strings***************
	//**can't be null otherwise error
	public static String vOrange;
	public static String vPink;
	public static String vBrown;
	public static String vPurple;
	public static String vCyan;
	//*******************************
	
	
	/**
	 * Creates a Java code scanner with the given color provider.
	 *
	 * @param provider the color provider
	 */
	public JavaCodeScanner(JavaColorProvider provider) {
		
		//******************************
		//getting variable data
		FileReader file_input;
		try {
			file_input = new FileReader("sthighlighter.dat");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			FileWriter output;
			try {
				output = new FileWriter("sthighlighter.dat");
				BufferedWriter bw0 = new BufferedWriter(output);
				bw0.write("oRANGE");
				bw0.newLine();
				bw0.write("bROWN");
				bw0.newLine();
				bw0.write("pURPLE");
				bw0.newLine();
				bw0.write("cYAN");
				bw0.newLine();
				bw0.write("pINK");
				bw0.close();
			    output.close();
			    
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		    
		}
		try {
			file_input = new FileReader("sthighlighter.dat");
			BufferedReader br = new BufferedReader(file_input);
			
			try {
				vOrange = br.readLine();
				vBrown = br.readLine();
				vPurple = br.readLine();
				vCyan = br.readLine();
				vPink = br.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				vOrange = "oRANGE";
				vBrown = "bROWN";
				vPurple = "pURPLE";
				vCyan = "cYAN";
				vPink = "pINK";
			}
			

		    try {
				br.close();
				file_input.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//*******************************

		IToken keyword= new Token(new TextAttribute(provider.getColor(JavaColorProvider.KEYWORD)));
		IToken type= new Token(new TextAttribute(provider.getColor(JavaColorProvider.TYPE)));
		IToken string= new Token(new TextAttribute(provider.getColor(JavaColorProvider.STRING)));
		IToken comment= new Token(new TextAttribute(provider.getColor(JavaColorProvider.SINGLE_LINE_COMMENT)));
		IToken other= new Token(new TextAttribute(provider.getColor(JavaColorProvider.DEFAULT)));
		
		
		//My Token*******************************
		IToken orange= new Token(new TextAttribute(provider.getColor(JavaColorProvider.ORANGE)));
		IToken pink= new Token(new TextAttribute(provider.getColor(JavaColorProvider.PINK)));
		IToken purple= new Token(new TextAttribute(provider.getColor(JavaColorProvider.PURPLE)));
		IToken brown= new Token(new TextAttribute(provider.getColor(JavaColorProvider.BROWN)));
		IToken cyan= new Token(new TextAttribute(provider.getColor(JavaColorProvider.CYAN)));
		//*******************************
		
		List rules= new ArrayList();

		// Add rule for single line comments.
		rules.add(new EndOfLineRule("//", comment)); //$NON-NLS-1$

		// Add rule for strings and character constants.
		rules.add(new SingleLineRule("\"", "\"", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
		rules.add(new SingleLineRule("'", "'", string, '\\')); //$NON-NLS-2$ //$NON-NLS-1$
		
		
		// Add generic whitespace rule.
		rules.add(new WhitespaceRule(new JavaWhitespaceDetector()));
		
		/*
		//testing variables******************
		vOrange = "ORANGE";
		vPink = "PINK";
		vBrown = "BROWN";
		vCyan = "CYAN";
		vPurple = "PURPLE";
		//*******************************
		*/
		
		// Add word rule for keywords, types, and constants.
		WordRule wordRule= new WordRule(new JavaWordDetector(), other);
		for (int i= 0; i < fgKeywords.length; i++)
			wordRule.addWord(fgKeywords[i], keyword);
		for (int i= 0; i < fgTypes.length; i++)
			wordRule.addWord(fgTypes[i], type);
		for (int i= 0; i < fgConstants.length; i++)
			wordRule.addWord(fgConstants[i], type);
		
		//myVariables*******************
		wordRule.addWord(vOrange, orange);
		wordRule.addWord(vPink, pink);
		wordRule.addWord(vPurple, purple);
		wordRule.addWord(vBrown, brown);
		wordRule.addWord(vCyan, cyan);
		//*******************************
		
		rules.add(wordRule);

		IRule[] result= new IRule[rules.size()];
		rules.toArray(result);
		setRules(result);
	}
	
	public static void setColor(String string, String color) throws IOException {
		
		FileWriter output = new FileWriter("sthighlighter.dat");
	    BufferedWriter bw = new BufferedWriter(output);
		
	    if (color != "Orange" && string == vOrange){
	    	vOrange = "oRANGE";
	    	bw.write("oRANGE");
	    	bw.newLine();
	    }
	    else if(color == "Orange"){
	    	bw.write(string);
	    	bw.newLine();
	    	vOrange = string;
	    }
	    else{
	    	bw.write(vOrange);
	    	bw.newLine();
	    }
	    //*****************************************************
	    if(color == "Brown"){
	    	bw.write(string);
	    	bw.newLine();
	    	vBrown = string;
	    }	    
	    else{
	    	if (string == "123"){
		    	vBrown = "wwwwwwwwwwww";
		    	bw.write("wwwwwwwwwwww");
		    	bw.newLine();
		    }
	    	else{
	    	bw.write(vBrown);
	    	bw.newLine();
	    	}
	    }
	    //*****************************************************

	    if (color != "Purple" && string == vPurple){
	    	vPurple = "pURPLE";
	    	bw.write("pURPLE");
	    	bw.newLine();
	    }
	    else if(color == "Purple"){
	    	bw.write(string);
	    	bw.newLine();
	    	vPurple = string;
	    }
	    else{
	    	bw.write(vPurple);
	    	bw.newLine();
	    }
	    
	    if (color != "Cyan" && string == vCyan){
	    	vCyan = "cYAN";
	    	bw.write("cYAN");
	    	bw.newLine();
	    }
	    else if(color == "Cyan"){
	    	bw.write(string);
	    	bw.newLine();
	    	vCyan = string;
	    }
	    else{
	    	bw.write(vCyan);
	    	bw.newLine();
	    }
	    
	    if (color != "Pink" && string == vPink){
	    	vPink = "pINK";
	    	bw.write("vPINK");
	    	bw.newLine();
	    }
	    else if(color == "Pink"){
	    	bw.write(string);
	    	bw.newLine();
	    	vPink = string;
	    }
	    else{
	    	bw.write(vPink);
	    	bw.newLine();
	    }
	    /*
	    bw.write(string);
		bw.newLine();
		bw.write(vBrown);
		bw.newLine();
		bw.write(vPurple);
		bw.newLine();
		bw.write(vCyan);
		bw.newLine();
		bw.write(vPink);
		*/
		bw.close();
	    output.close();
	}

	public static void printing(){
		System.out.println("orange:" + vOrange);
		System.out.println("brown:" + vBrown);
		System.out.println("purple:" + vPurple);
		System.out.println("cyan:" + vCyan);
		System.out.println("pink:" + vPink);
		
	}
	public static void printLocal(){
		
		FileReader file_input;
		try {
			file_input = new FileReader("sthighlighter.dat");
			BufferedReader br = new BufferedReader(file_input);
			
			for (int i =0; i<5; i++){
				try {
					System.out.println(br.readLine());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public static void changeColorVariable(String string, String color) throws IOException {
	
		if (color == "Orange")
			vOrange = string;
		if (color == "Brown")
			vBrown = string;
		if (color == "Purple")
			vPurple = string;
		if (color == "Pink")
			vPink = string;
		if (color == "Cyan")
			vCyan = string;
	}
	
	public static void readLocal(){
		
		try {
			FileReader file_input = new FileReader("sthighlighter.dat");
			BufferedReader br = new BufferedReader(file_input);
			
			try {
				vOrange = br.readLine();
				vBrown = br.readLine();
				vPurple = br.readLine();
				vCyan = br.readLine();
				vPink = br.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				vOrange = "oRANGE";
				vBrown = "bROWN";
				vPurple = "pURPLE";
				vCyan = "cYAN";
				vPink = "pINK";
			}
			
		    try {
				br.close();
				file_input.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
